package multiconnector.broadcast;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

public abstract class BroadcastSocket implements Runnable {

	private static final String LOG_TAG = BroadcastSocket.class.getName();
	private DatagramSocket mSocket;

	private final InetAddress broadcastAddress;
	private int port;
	private boolean running;
	private Thread thread;

	public BroadcastSocket(InetAddress inetAddress, int port) {
		broadcastAddress = inetAddress;
		this.port = port;
		try {
			mSocket = createSocket();
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "error while creating sockets");
			return;
		}
	}

	/**
	 * whether service is running
	 *
	 * @return
	 */
	public boolean isRunning() {
		return running;
	}


	/**
	 * start unless already running
	 */
	public void start() {
		if (!running) {
			running = true;
			thread = new Thread(this);
			thread.start();
		}
	}

	public void stop() {
		running = false;
	}

	public abstract DatagramSocket createSocket() throws SocketException;

	public DatagramSocket getSocket() {
		return mSocket;
	}

	public InetAddress getBroadcastAddress() {
		return broadcastAddress;
	}


	public int getPort() {
		return port;
	}


}
